Imports System.Data.SqlClient
Public Class Estado : Implements IBusquedaFiltrado
    Private IdEstado As Integer
    Private Descrip As String
    Public Property identificador() As Integer
        Get
            Return IdEstado
        End Get
        Set(ByVal value As Integer)
            IdEstado = value
        End Set
    End Property
    Public Property Descripcion() As String
        Get
            Return Descrip
        End Get
        Set(ByVal value As String)
            Descrip = value
        End Set
    End Property

    Public Sub New()
        IdEstado = 0
        Descrip = ""
    End Sub
    Public Function Buscar(ByVal id As Integer, ByRef con As ConexionBD) As Boolean _
                    Implements IBusquedaFiltrado.Buscar

        Dim comando As SqlDataAdapter
        Dim rsEstado As DataTableReader
        Dim dato As DataTable
        Dim sql As String
        Dim lResp As Boolean = False

        sql = String.Format("SELECT * FROM ESTADOS WHERE idEstado ={0} ", id)
        comando = New SqlDataAdapter(sql, con.ConexionBase)
        dato = New DataTable("estados")
        comando.Fill(dato)
        rsEstado = dato.CreateDataReader()
        comando.Dispose()
        dato.Dispose()

        If rsEstado.Read Then
            IdEstado = System.Convert.ToInt16(rsEstado("IdEstado"))
            Descrip = rsEstado("Descrip").ToString
            lResp = True
        Else
            lResp = False
        End If
        rsEstado.Close()
        Return lResp

    End Function

    Public Function Agregar(ByRef con As ConexionBD) As Boolean Implements IBusquedaFiltrado.Agregar
        Dim sql As String = ""
        Dim comando As SqlCommand = Nothing

        sql = "INSERT INTO Estados (IdEsatdo,Descrip)" + _
              "  VALUES({0},'{1}')"
        sql = String.Format(sql, IdEstado, Descrip)
        comando = New SqlCommand(sql, con.ConexionBase)
        If comando.ExecuteNonQuery() > 0 Then
            Return True
        Else
            Return False
        End If

    End Function

    Public Function Modificar(ByRef con As ConexionBD) As Boolean Implements IBusquedaFiltrado.Modificar
        Dim sql As String = ""
        Dim comando As SqlCommand = Nothing

        sql = "UPDATE Estados " + _
              "  SET Descrip = '{0}'," + _
              "  WHERE IdEstado = {1}"
        sql = String.Format(sql, Descrip, IdEstado)
        comando = New SqlCommand(sql, con.ConexionBase)
        If comando.ExecuteNonQuery() > 0 Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Function Eliminar(ByVal id As Integer, ByRef con As ConexionBD) As Boolean _
            Implements IBusquedaFiltrado.Eliminar

        Dim sql As String = ""
        Dim comando As SqlCommand = Nothing

        sql = "DELETE FROM Estados WHERE IdEstado ={0}"
        sql = String.Format(sql, id)
        comando = New SqlCommand(sql, con.ConexionBase)
        If comando.ExecuteNonQuery() > 0 Then
            Return True
        Else
            Return False
        End If

    End Function
    Public Function BuscarDatos(ByRef con As ConexionBD) As DataSet

        Dim sql As String = ""
        Dim adapter As SqlDataAdapter = Nothing
        Dim datos As DataSet = New DataSet()

        sql = String.Format("SELECT * FROM ESTADOS WHERE afecta={0}", 1)
        adapter = New SqlDataAdapter(sql, con.ConexionBase)
        adapter.Fill(datos, "estados")

        Return datos
    End Function
    Public Function BuscarId(ByVal DescEst As String, ByRef con As ConexionBD) As Boolean

        Dim comando As SqlCommand
        Dim rs As SqlDataReader
        Dim sql As String
        Dim lResp As Boolean = False

        sql = String.Format("SELECT * FROM ESTADOS WHERE Descrip='{0}'", DescEst)
        comando = New SqlCommand(sql, con.ConexionBase)
        rs = comando.ExecuteReader()
        comando.Dispose()


        If rs.Read Then
            IdEstado = System.Convert.ToInt16(rs("idEstado"))
            Descrip = rs("Descrip").ToString
            lResp = True
        Else
            lResp = False
        End If
        rs.Close()
        Return lResp

    End Function
End Class
